#!/bin/bash -e

source $PATH_SCRIPTS/get-package.sh
source $PATH_SCRIPTS/tool-util.sh


# present the header
echo	"


=====================================================================

           Utility to generate level 1 tools
 
=====================================================================



"

echo	"
Set up a good working environment by creating 
two new startup files for the bash shell."

cat > ~/.bash_profile << EOF
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF

echo	"
The set +h command turns off bash's hash function"
echo	"
Setting the user file-creation mask (umask) to 
022 ensures that newly created files 
and directories are only writable by 
their owner, but are readable and 
executable by anyone"
echo	"
The LC_ALL variable controls the localization of certain programs"
echo	"
The LFS_TGT variable sets a non-default,
but compatible machine description for use 
when building our cross compiler and linker 
and when cross compiling our temporary toolchain."
echo	"
By putting /tools/bin ahead of the standard PATH, 
all the programs installed in Chapter 5 are 
picked up by the shell immediately after 
their installation."
cat > ~/.bashrc << EOF
set +h
umask 022
JAIL_NAME=$JAIL_NAME
JAIL_PATH=$JAIL_PATH
LC_ALL=POSIX
LFS_TGT=$(uname -m)-$JAIL_NAME-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
MAKEFLAGS=$MAKEFLAGS
export LC_ALL LFS_TGT PATH MAKEFLAGS
EOF


set +h
umask 022
LC_ALL=POSIX
LFS_TGT=$(uname -m)-$JAIL_NAME-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LC_ALL LFS_TGT PATH MAKEFLAGS




# ===============================================================
initDir "5.4." "binutils-2.22"
	pushd $SRC_DIR > /dev/null
		patch -Np1 -i $PATH_PKGCACHE/binutils-2.22-build_fix-1.patch >> $CRTLOG
	popd > /dev/null
	$SRC_DIR/configure     \
		--prefix=/tools            \
		--with-sysroot=$JAIL_PATH  \
		--with-lib-path=/tools/lib \
		--target=$LFS_TGT          \
		--disable-nls              \
		--disable-werror >> $CRTLOG
	make >> $CRTLOG
	case $(uname -m) in
	  x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;;
	esac
	make install >> $CRTLOG
rDir
# ===============================================================



# ===============================================================
initDir "5.5." "gcc-4.7.2"
	pushd $SRC_DIR > /dev/null

		# GCC now requires the GMP, MPFR and MPC packages
		getCachedPackage "mpfr-2.4.2" $SRC_DIR/mpfr
		getCachedPackage "gmp-4.3.2" $SRC_DIR/gmp
		getCachedPackage "mpc-0.8.1" $SRC_DIR/mpc

		# The following command will change the location of 
		# GCC's default dynamic linker to use the one installed 
		# in /tools. It also removes /usr/include from GCC's 
		# include search path
		for file in \
		 $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
		do
		  cp -uv $file{,.orig}
		  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
			  -e 's@/usr@/tools@g' $file.orig > $file
		  echo '
		#undef STANDARD_STARTFILE_PREFIX_1
		#undef STANDARD_STARTFILE_PREFIX_2
		#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
		#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
		  touch $file.orig
		done

		# GCC doesn't detect stack protection correctly, which causes
		# problems for the build of Glibc-2.16.0, so fix that
		# by issuing the following command:
		sed -i '/k prot/agcc_cv_libc_provides_ssp=yes' gcc/configure

	popd > /dev/null


	# Prepare GCC for compilation: 
	$SRC_DIR/configure         \
		--target=$LFS_TGT          \
		--prefix=/tools            \
		--with-sysroot=$LFS        \
		--with-newlib              \
		--without-headers          \
		--with-local-prefix=/tools \
		--with-native-system-header-dir=/tools/include \
		--disable-nls              \
		--disable-shared           \
		--disable-multilib         \
		--disable-decimal-float    \
		--disable-threads          \
		--disable-libmudflap       \
		--disable-libssp           \
		--disable-libgomp          \
		--disable-libquadmath      \
		--enable-languages=c >> $CRTLOG
		
		
	make >> $CRTLOG
	make install >> $CRTLOG

	#Using --disable-shared means that the libgcc_eh.a file 
	#isn't created and installed. The Glibc package depends on 
	#this library as it uses -lgcc_eh within its build system. 
	#This dependency can be satisfied by creating a symlink to
	#libgcc.a, since that file will end up containing the objects 
	#normally contained in libgcc_eh.a: 
	ln -sv libgcc.a `$LFS_TGT-gcc -print-libgcc-file-name | sed 's/libgcc/&_eh/'`

rDir
# ===============================================================





# ===============================================================
# Linux headers
initDir "5.6." "linux-3.6.2"
	pushd	$SRC_DIR > /dev/null
		# Make sure there are no stale files and dependencies 
		# lying around from previous activity:
		make mrproper >> $CRTLOG

		# Now test and extract the user-visible kernel 
		# headers from the source. 
		# They are placed in an intermediate 
		# local directory and copied to the needed 
		# location because the extraction process removes 
		# any existing files in the target directory.

		make headers_check >> $CRTLOG
		make INSTALL_HDR_PATH=dest headers_install >> $CRTLOG
		cp -rv dest/include/* /tools/include
	popd > /dev/null
rDir
# ===============================================================




# ===============================================================
# Glibc-2.16.0 
initDir "5.7." "glibc-2.16.0"
	pushd	$SRC_DIR > /dev/null
		if [ ! -r /usr/include/rpc/types.h ]; then 
		  sudo -c 'mkdir -p /usr/include/rpc'
		  sudo -c 'cp -v sunrpc/rpc/*.h /usr/include/rpc'
		fi

		#  Fix an issue when building Glibc-2.16.0 with GCC-4.7.2: 
		sed -i 's/ -lgcc_s//' Makeconfig
	popd > /dev/null

	# Next, prepare Glibc for compilation:

	$SRC_DIR/configure                             \
		  --prefix=/tools                                 \
		  --host=$LFS_TGT                                 \
		  --build=$($SRC_DIR/scripts/config.guess) \
		  --disable-profile                               \
		  --enable-add-ons                                \
		  --enable-kernel=2.6.25                          \
		  --with-headers=/tools/include                   \
		  libc_cv_forced_unwind=yes                       \
		  libc_cv_ctors_header=yes                        \
		  libc_cv_c_cleanup=yes  >> $CRTLOG
finishDir
# ===============================================================




# ===============================================================
# CHECKPOINT

cd $JAIL_PATH/build-dir

echo	"


At this point, it is imperative to stop and ensure that the 
basic functions (compiling and linking) of the new 
toolchain are working as expected.

"
echo 'main(){}' > dummy.c
$LFS_TGT-gcc dummy.c
readelf -l a.out | grep ': /tools'

echo	"
If everything is working correctly, there should be no errors, 
and the output of the last command will be of the form:
[Requesting program interpreter: /tools/lib/ld-linux.so.2]
Note that /tools/lib, or /tools/lib64 for 64-bit machines appears 
as the prefix of the dynamic linker.

If the output is not shown as above or there was no output at all, 
then something is wrong. Investigate and retrace the steps to
find out where the problem is and correct it. This issue must 
be resolved before continuing on.

Press CTRL+C to stop.
Press RETURN to continue.
"

# Once all is well, clean up the test files:
rm -v "dummy.c" "a.out"

read dummy

# CHECKPOINT
# ===============================================================






# present the footer
echo	"


=====================================================================

           Utility to generate level 1 tools done
 
=====================================================================



"
